package com.zz.framework.core.ext

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created on 2020/3/10 1:39 PM.
 * @version: V1.0
 */

fun Any.toJson() = GsonBuilder().setPrettyPrinting().create().toJson(this)

inline fun <reified T> String.fromJson() = Gson().fromJson<T>(this, object : TypeToken<T>() {}.type)

inline fun <reified T> String.fromJsonNullable() = try {
    Gson().fromJson<T>(this, object : TypeToken<T>() {}.type)
}catch (e:Exception){
    e.printStackTrace()
    null
}


inline fun <reified T> String.toBean() = Gson().fromJson<T>(this, object : TypeToken<T>() {}.type)


inline fun String.toHashMap() = this.fromJson<HashMap<String,Any>>()

inline fun Any.toHashMap() = this.toJson().fromJson<HashMap<String,Any>>()


